/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.util;

import com.sun.mail.smtp.SMTPTransport;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author LUCAS
 */
public class SmtpUtil {

    private static final String SMTP_AUTH_USER = "cgbmembro@gmail.com";
    private static final String SMTP_AUTH_PWD = "cognitivegames";
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String PAGE = "activateuser.xhtml?activationCode=";

    public static void sendEmail(String email, String activationCode, String domain, ResourceBundle bundle) throws MessagingException {
        Properties props = System.getProperties();
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        Session session = Session.getInstance(props, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(email, false));
        msg.setSubject(bundle.getString("email.subject"));
        msg.setText(bundle.getString("email.text") + domain + PAGE + activationCode);
        msg.setHeader("CGB", "Make be real!");
        msg.setSentDate(new Date());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtps");
        transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER, SMTP_AUTH_PWD);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }
}
